package com.imooc.mall.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.mall.common.Constant;
import com.imooc.mall.exception.ImoocMallException;
import com.imooc.mall.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.request.ProductRequestQuery;
import com.imooc.mall.model.dao.ProductMapper;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.model.request.AddProductAdd;
import com.imooc.mall.model.request.ProductRequest;
import com.imooc.mall.model.vo.CategoryVO;
import com.imooc.mall.service.CategoryService;
import com.imooc.mall.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("productService")
public class ProductServiceImpl implements ProductService {

    @Resource
    private ProductMapper productMapper;

    @Autowired
    private CategoryService categoryService;

    @Override
    public void AddProductAdd(AddProductAdd addProductAdd){
        Product product = new Product();
        BeanUtils.copyProperties(addProductAdd,product);
        Product OldProduct = productMapper.selectByName(addProductAdd.getName());
        if(OldProduct != null){
            throw new ImoocMallException(ImoocMallExceptionEnum.NAME_EXISTED);
        }
        int count = productMapper.insertSelective(product);
        if (count == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.INSERT_FAILED);
        }
    }

    @Override
    public void UpdateProduct(Product product){
        Product productOld = productMapper.selectByName(product.getName());
        if (productOld!=null && !productOld.getId().equals(product.getId())){
          throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
        int count = productMapper.updateByPrimaryKeySelective(product);
        if (count == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }
    }

    @Override
    public void DeleteProduct(Integer id){
        Product product = productMapper.selectByPrimaryKey(id);
        if (product == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
        int i = productMapper.deleteByPrimaryKey(id);
        if (i == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }
    }

    @Override
    public void BatchUpdateSellStatus(Integer[] ids , Integer sellStatus){
        int i = productMapper.updateBySellStatus(ids, sellStatus);
        if (i == 0){
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }

    }


    @Override
    public PageInfo listAdmin(Integer pageSize, Integer pageNum){
        Page<Product>  products= PageHelper.startPage(pageNum,pageSize);
        List<Product> listAll = productMapper.selectByListAll();
        PageInfo tPageInfo = new PageInfo( listAll);
        return tPageInfo;
    }

    @Override
    public Product detail(Integer id){
        Product product = productMapper.selectByPrimaryKey(id);
        if(product == null){
            throw new ImoocMallException(ImoocMallExceptionEnum.PRODUCT_NUME);
        }
        return product;
    }


    @Override
    public PageInfo list(ProductRequest productRequest){

        ProductRequestQuery productRequestQuery = new ProductRequestQuery();

        // 搜索名字类型
        if (!StringUtils.isEmpty(productRequest.getKeyword())){
            String string = new StringBuilder().append("%").append(productRequest.getKeyword()).append("%").toString();
            productRequestQuery.setKeyword(string);
        }

        // 目录处理，如果查一个目录不仅需要拿出商品，还要把所有子目录的商品都查出来，所有要拿出一个目录的id的List
        if (productRequest.getCategoryId()!=null){
            List<CategoryVO> categoryVOList = categoryService.listCategoryForCustomer(productRequest.getCategoryId());
            List<Integer> categoryIds = new ArrayList<Integer>();
            // 把这个对象的ID和子类对象一起存放到这个对象中
            categoryIds.add(productRequest.getCategoryId());
            // 拿出这个属性中的ID
            GetRequestCategory(categoryVOList,categoryIds);
            productRequestQuery.setCategoryIds(categoryIds);
        }

        // 排序处理
        String orderBy = productRequest.getOrderBy();
        // 不统一让前端传入什么就是用什么。
        if (Constant.ProductListOrderBy.PRICE_ASC_DESC.contains(orderBy)){
            PageHelper.startPage(productRequest.getPageNum(),productRequest.getPageSize(),orderBy);
        }else {
            PageHelper.startPage(productRequest.getPageNum(),productRequest.getPageSize());
        }
        List<Product> productList = productMapper.selectList(productRequestQuery);

        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        return pageInfo;
    }

    private void GetRequestCategory(List<CategoryVO> categoryVOList, List<Integer> categoryIds){
            for (int i = 0; i < categoryVOList.size(); i++) {
                CategoryVO categoryVO =  categoryVOList.get(i);
                if (categoryVO!= null){
                    categoryIds.add(categoryVO.getId());
                    GetRequestCategory(categoryVO.getChildCategory(),categoryIds);
                }
            }
        }
}
